﻿using System;
using Cms.Application.Admin.Presenters;
using Cms.Application.Admin.Views;
using Cms.Utilities;
using Cms.Web.Framework.Controls;

namespace Cms.Web.UI.Admin
{
    public partial class Update : AdminPage, IUpdateView
    {
        private UpdatePresenter _presenter;
        private IUpdater _updater;

        #region IUpdateView Members

        public void DisplayServiceUnavailableAlert()
        {
            pnlServiceUnavailable.Visible = true;
        }


        public void DisplaySystemIsUpToDateMessage()
        {
            pnlSystemIsUpToDate.Visible = true;
        }


        public void DisplayUpdate(string versionInfo)
        {
            pnlUpdate.Visible = true;
            litLatestVersion.Text = versionInfo;
        }


        public string ContextTranslationFile
        {
            get { return "Admin/Translations/Update.xml"; }
        }

        #endregion

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            _presenter = new UpdatePresenter(this);
            _updater = new Updater();
            HeadingText = GetText(this, "Heading");
            if (Page.IsPostBack)
                return;
            _presenter.Initialize();
            HeadingIcon = litIcon.Text;
            litCurrentVersion.Text = _updater.GetCurrentVersion().Full;
            if (Request.QueryString["state"] != "upgradecompleted")
                return;
            pnlCheck.Visible = false;
            pnlUpgradeCompleted.Visible = true;
            litNewCurrentVersion.Text = _updater.GetCurrentVersion().Full;
        }


        protected void lbCheck_Click(object sender, EventArgs e)
        {
            pnlServiceUnavailable.Visible = false;
            pnlSystemIsUpToDate.Visible = false;
            pnlUpdate.Visible = false;
            _presenter.CheckLatestVersion();
        }


        protected void lbUpgrade_Click(object sender, EventArgs e)
        {
            Response.Redirect("ProcessUpgrade.aspx?LatestVersion=" + Server.UrlEncode(_updater.GetLatestVersion().Full));
        }
    }
}